package com.yicheng.statistics.actor.analysis

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.Props
import com.yicheng.statistics.actor.SaveHtaData
import akka.actor.ActorRef
import com.yicheng.statistics.common.TimeUtils
import com.yicheng.statistics.repo.cassandra.DataInfoDB
import scala.util.Success
import scala.util.Failure
import java.util.Date
import org.joda.time.DateTime
import com.yicheng.statistics.repo.RTAModel.VehicleMileage
import com.yicheng.statistics.service.HTAService

/**
 * 里程统计分析
 */
class Mileage extends Actor with ActorLogging{
  
  val saveHtaDataActor:ActorRef = context.child("saveHtaData").
    getOrElse(context.actorOf(Props[SaveHtaData], "saveHtaData"))
  
  import scala.concurrent.ExecutionContext.Implicits.global
  
  def receive: Receive = {
    case (deviceId:String,deviceType:Int) =>
      log.info("里程统计 deviceId "+deviceId)
      
      var mileage = MileageData(None, None, None, None, None, None, None, None,None,None)//(startmil, endmil) 开始里程(第一条) 结束里程(最后一条)
      var flag = 0
      
      HTAService.getOneDayDataInfo(deviceType, deviceId) onComplete { 
        case Success(result) =>
          result.map( di => {
            val data = di.data_vehicle.overallData.get//.get.overallData.get
            val pos = di.data_vehicle.posData.get
            log.info("query data is "+di)
            if(flag == 0) {//第一条数据设置开始
              mileage = MileageData(Some(data.currentMileage), 
                         Some(pos.getLatitude),
                         Some(pos.getLongitude),
                         Some(di.data_time),
                         None,
                         None,
                         None,
                         None,
                         Some(data.socStatus),
                         None)
              flag = 1
            }else{
              if(data.socStatus > mileage.socStatus.get) {//充电了后面的电量比前面的大
                mileage = MileageData(mileage.start,
                                   mileage.startLat,
                                   mileage.startLon,
                                   mileage.startTime,
                                   Some(data.currentMileage), 
                                   Some(pos.getLatitude),
                                   Some(pos.getLongitude),
                                   Some(di.data_time),
                                   Some(data.socStatus),
                                   mileage.totalSoc
                                   )
              }else{
                val total = mileage.totalSoc.get + (mileage.socStatus.get - data.socStatus)
                mileage = MileageData(mileage.start,
                                   mileage.startLat,
                                   mileage.startLon,
                                   mileage.startTime,
                                   Some(data.currentMileage), 
                                   Some(pos.getLatitude),
                                   Some(pos.getLongitude),
                                   Some(di.data_time),
                                   Some(data.socStatus),
                                   Some(mileage.totalSoc.get + total)
                                   )
              }
              
            }
          })
        case Failure(error) =>
                log.error(error.getMessage) 
      }
      
      if( !mileage.start.isEmpty){
        val vehicleMileage = VehicleMileage(deviceType,
                                          deviceId,
                                          mileage.start,
                                          mileage.end,
                                          mileage.startTime.get,
                                          mileage.endTime.get,
                                          DateTime.now().toDate(),
                                          DateTime.now().toDate(),
                                          None,
                                          mileage.startLon,
                                          mileage.startLat,
                                          mileage.endLon,
                                          mileage.endLat,
                                          Some(mileage.totalSoc.get.toString())
                                          )
      
        saveHtaDataActor ! vehicleMileage//保存数据
      }
      
      
    case _ => log.info("recive data : ")
  }
}

case class MileageData(
    start:Option[Float], //开始里程
    startLat:Option[Double],
    startLon:Option[Double],
    startTime:Option[Date],
    end:Option[Float],//结束里程
    endLat:Option[Double],
    endLon:Option[Double],
    endTime:Option[Date],
    socStatus:Option[Int],//电池电量百分比
    totalSoc:Option[Int]
    )